package com.hsl.test;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.Notifications;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.wm.ToolWindow;
import javax.swing.*;

public class MyToolWindow {

    private JButton hideButton;
    private JButton dialogButton;
    private JButton cancelButton;

    private JPanel myToolWindowContent;

    private JLabel ip = new JLabel("IP：");
    private JTextField ipContent = new JTextField();

    private JLabel port = new JLabel("端口：");
    private JTextField portContent = new JTextField();

    private JLabel length = new JLabel("长度位：");
    private JTextField lengthContent = new JTextField();

    private JTextArea textArea;




    public MyToolWindow(ToolWindow toolWindow) {

        init();

        hideButton.addActionListener(e -> toolWindow.hide(null));
    }

    private void init() {
        dialogButton = new JButton("获得连接");
        dialogButton.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        dialogButton.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        dialogButton.addActionListener(e -> {

            String ipContentText = ipContent.getText();
            String portContentText = portContent.getText();
            String lengthContentText = lengthContent.getText();
            String str = "创建连接: ip: " + ipContentText + "  port: " + portContentText + "  headLength: " + lengthContentText;
            notifyMsg(str);
        });

        hideButton = new JButton("cancel");
        hideButton.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        hideButton.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        hideButton.addActionListener(e -> {
            notifyMsg("cancel");
        });

        cancelButton = new JButton("断开连接");
        cancelButton.addActionListener(e -> {
            notifyMsg("cancel connect...");
        });

        textArea = new JTextArea(5,10);
        textArea.setLineWrap(true);


        myToolWindowContent = new JPanel();

        myToolWindowContent.add(ip);
        myToolWindowContent.add(ipContent);

        myToolWindowContent.add(port);
        myToolWindowContent.add(portContent);

        myToolWindowContent.add(length);
        myToolWindowContent.add(lengthContent);

        myToolWindowContent.add(dialogButton);
        myToolWindowContent.add(hideButton);
        myToolWindowContent.add(cancelButton);

        myToolWindowContent.add(textArea);


    }

    public JPanel getContent() {
        return myToolWindowContent;
    }



    public void notifyMsg(String msg)
    {
        NotificationGroup notificationGroup = new NotificationGroup("testid", NotificationDisplayType.BALLOON, false);
        /**
         * content :  通知内容
         * type  ：通知的类型，warning,info,error
         */
        Notification notification = notificationGroup.createNotification(msg, MessageType.INFO);
        Notifications.Bus.notify(notification);
    }

}